﻿using M_ApiResult;
using M_Manufacture.API.Application.Command.ProductionPlanCommand;
using M_Manufacture.Domain;
using M_Manufacture.Domain.DTO;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M_Manufacture.API.Controllers
{
    /// <summary>
    /// 产品控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductionController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        public ProductionController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 创建生产计划
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateProductionPlan(CreateProductionPlanCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<ProductionPlan>>> Show([FromQuery] ShowProductionPlanCommand command)
        {
            ApiResult<PageDto<ProductionPlan>> result = new ApiResult<PageDto<ProductionPlan>>();
            try
            {

                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 作废
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ZuoFeiPro([FromQuery] ZuoFeiCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);

            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "作废失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填生产计划
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductionPlan>> GetByPro([FromQuery] GetByidCommand command)
        {
            ApiResult<ProductionPlan> result = new ApiResult<ProductionPlan>();
            try
            {
                result.Data = await mediator.Send(command);

            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "反填生产计划信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 排程
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateProductionPlan([FromQuery]UpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
